Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix bug and add test case #2168

Merged
merged 5 commits into from
Aug 23, 2024
Merged

fix: Fix bug and add test case #2168

merged 5 commits into from
Aug 23, 2024

Conversation

p-offtermatt
Copy link
Contributor

@p-offtermatt p-offtermatt commented Aug 23, 2024

Description

Closes: #2169

Bug reported by @fastfadingviolets via Slack

In the test scenario presented in the new e2e test, namely:

  • provider chain with max_validators=5, max_provider_consensus_validators=4
  • validators with powers 30, 29, 20, 10, 7, 4
  • a consumer chain with top N=100
  • then validators 30, 29, 20, 10 should have to validate on the consumer chain.

The previous behaviour was that validator 10 did not have to validate, and only 30, 29, and 20 had to validate.

The bug was this:
In

err := stakingKeeper.IterateLastValidatorPowers(ctx, func(addr sdk.ValAddress, power int64) (stop bool) {
in the function GetLastBondedValidatorsUtil, the staking keeper method IterateLastValidatorPowers is called assuming that it returns validators sorted by power.
This is not true, so wrong validators would be returned, which caused wrong validators to be used when computing the consumer chain validator set.

The fix is in x/ccv/types/utils.go

tests/e2e contains a regression tests outlining the scenario explained above.

In the integration tests, a test actually depended on the broken behaviour, so it was adjusted.

Further, the mocks were adjusted, since GetLastBondedValidatorsUtil calls GetBondedValidatorsByPower instead of IterateLastValidatorPowers now, and doesn't need to take the powers as an input.


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • Included the correct type prefix in the PR title
  • Added ! to the type prefix if the change is state-machine breaking
  • Confirmed this PR does not introduce changes requiring state migrations, OR migration code has been added to consumer and/or provider modules
  • Targeted the correct branch (see PR Targeting)
  • Provided a link to the relevant issue or specification
  • Followed the guidelines for building SDK modules
  • Included the necessary unit and integration tests
  • Added a changelog entry to CHANGELOG.md
  • Included comments for documenting Go code
  • Updated the relevant documentation or specification
  • Reviewed "Files changed" and left comments if necessary
  • Confirmed all CI checks have passed
  • If this PR is library API breaking, bump the go.mod version string of the repo, and follow through on a new major release

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! the type prefix if the change is state-machine breaking
  • confirmed this PR does not introduce changes requiring state migrations, OR confirmed migration code has been added to consumer and/or provider modules
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage

Summary by CodeRabbit

  • New Features

    • Introduced new test configurations for inactive validators, enhancing the testing framework's flexibility.
    • Added logging for validator opting-in processes, improving traceability.
  • Bug Fixes

    • Enhanced assertions by sorting staked tokens in integration tests for more accurate validation.
  • Refactor

    • Simplified function calls in mock setups by removing unnecessary parameters across various tests.
    • Refactored logic for retrieving last bonded validators, improving efficiency and error handling.
  • Tests

    • Expanded test scenarios to include edge cases related to inactive validators and their configurations.

@github-actions github-actions bot added C:Testing Assigned automatically by the PR labeler C:x/provider Assigned automatically by the PR labeler C:x/types Assigned automatically by the PR labeler labels Aug 23, 2024
Comment on lines +628 to +630
for valId, val := range extraVals {
tr.validatorConfigs[valId] = val
}

Check warning

Code scanning / CodeQL

Iteration over map Warning test

Iteration over map may be a possible source of non-determinism
x/ccv/types/utils.go Fixed Show fixed Hide fixed
@github-actions github-actions bot added the C:x/consumer Assigned automatically by the PR labeler label Aug 23, 2024
@p-offtermatt p-offtermatt marked this pull request as ready for review August 23, 2024 11:05
@p-offtermatt p-offtermatt requested a review from a team as a code owner August 23, 2024 11:05
Copy link
Contributor

coderabbitai bot commented Aug 23, 2024

Walkthrough

Walkthrough

The changes introduce enhancements to the testing framework, including a new test configuration for scenarios with inactive validators, a function to retrieve additional validator configurations, and the simulation of validator actions within a blockchain context. These modifications aim to improve the robustness and flexibility of the e2e testing suite, particularly concerning top N validator scenarios.

Changes

File(s) Change Summary
tests/e2e/config.go Introduced InactiveValsExtraValsTestCfg constant, updated GetTestConfig, and added GetExtraValidatorConfigs function.
tests/e2e/main.go Added new entry for "inactive-vals-outside-max-validators" in stepChoices map with corresponding properties.
tests/e2e/steps.go Introduced stepsInactiveValsTopNReproduce function simulating validator actions.
tests/integration/partial_set_security_test.go Modified TestMinStake to sort staked tokens for assertions.
testutil/keeper/expectations.go Updated SetupMocksForLastBondedValidatorsExpectation to mock GetBondedValidatorsByPower.
testutil/keeper/unit_test_helpers.go Simplified call to SetupMocksForLastBondedValidatorsExpectation.
x/ccv/consumer/keeper/changeover_test.go Removed unused powers slice in TestChangeoverToConsumer.
x/ccv/provider/keeper/grpc_query_test.go Simplified mock setup by removing powers parameter from SetupMocksForLastBondedValidatorsExpectation.
x/ccv/types/utils.go Refactored GetLastBondedValidatorsUtil to streamline logic for retrieving bonded validators.

Assessment against linked issues

Objective Addressed Explanation
Ensure consumer chain validators match expected configuration (2169)
Validate changes to handling of inactive validators (2169)
Improve clarity and efficiency of mock setups in tests (2169)

Recent review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 62f159e and 27ef6dc.

Files selected for processing (2)
  • x/ccv/provider/keeper/partial_set_security.go (2 hunks)
  • x/ccv/types/utils.go (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • x/ccv/provider/keeper/partial_set_security.go
  • x/ccv/types/utils.go

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Outside diff range, codebase verification and nitpick comments (1)
testutil/keeper/unit_test_helpers.go (1)

225-225: Mismatch in function signature and call.

The function SetupMocksForLastBondedValidatorsExpectation is defined with four parameters, but the call in the test file uses only three arguments. This discrepancy needs to be addressed to ensure consistency between the function definition and its usage.

  • Function Definition: testutil/keeper/expectations.go, line 223
  • Function Call: testutil/keeper/unit_test_helpers.go, line 225
Analysis chain

Verify the function signature update.

Ensure that the SetupMocksForLastBondedValidatorsExpectation function signature has been updated to reflect this change in parameters.

Run the following script to verify the function signature:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the function signature of `SetupMocksForLastBondedValidatorsExpectation`.

# Test: Search for the function definition. Expect: Updated signature with a single integer argument.
ast-grep --lang go --pattern 'func SetupMocksForLastBondedValidatorsExpectation($_, $_, $_, $_)' 

Length of output: 1049

Comment on lines 132 to 135
// get the bonded validators from the staking module, sorted by power
bondedValidators, err := stakingKeeper.GetBondedValidatorsByPower(ctx)
if err != nil {
return nil, err
panic(fmt.Errorf("failed to get bonded validators: %w", err))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using panic for error handling.

Using panic for error handling can cause a chain halt. Consider returning an error instead to handle failures gracefully.

Apply this diff to replace the panic with error handling:

 if err != nil {
-  panic(fmt.Errorf("failed to get bonded validators: %w", err))
+  return nil, fmt.Errorf("failed to get bonded validators: %w", err)
}
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// get the bonded validators from the staking module, sorted by power
bondedValidators, err := stakingKeeper.GetBondedValidatorsByPower(ctx)
if err != nil {
return nil, err
panic(fmt.Errorf("failed to get bonded validators: %w", err))
// get the bonded validators from the staking module, sorted by power
bondedValidators, err := stakingKeeper.GetBondedValidatorsByPower(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get bonded validators: %w", err)
Tools
GitHub Check: CodeQL

[warning] 135-135: Panic in BeginBock or EndBlock consensus methods
Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt

Copy link
Contributor

@MSalopek MSalopek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving the change, I have no objections to testing changes.

return i >= int(maxVals) // stop iteration if true
})
// get the bonded validators from the staking module, sorted by power
bondedValidators, err := stakingKeeper.GetBondedValidatorsByPower(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was super subtle. Great catch!

x/ccv/types/utils.go Outdated Show resolved Hide resolved
x/ccv/provider/keeper/partial_set_security.go Outdated Show resolved Hide resolved
x/ccv/provider/keeper/partial_set_security.go Outdated Show resolved Hide resolved
Copy link
Contributor

@mpoke mpoke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work @p-offtermatt

@p-offtermatt p-offtermatt added this pull request to the merge queue Aug 23, 2024
@p-offtermatt p-offtermatt removed this pull request from the merge queue due to a manual request Aug 23, 2024
@p-offtermatt p-offtermatt added this pull request to the merge queue Aug 23, 2024
Merged via the queue into main with commit 3a0c55f Aug 23, 2024
15 of 16 checks passed
@p-offtermatt p-offtermatt deleted the ph/add-test-inactive-vals branch August 23, 2024 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:Testing Assigned automatically by the PR labeler C:x/consumer Assigned automatically by the PR labeler C:x/provider Assigned automatically by the PR labeler C:x/types Assigned automatically by the PR labeler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Consumer chain validators are not as expected for top N = 100
3 participants